package zouran.controller;

import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zouran.entity.EarthquakeInfo;
import zouran.entity.SelectCondition;
import zouran.exception.MyRunTimeException;
import zouran.result.ErrorMessage;
import zouran.result.ResultMap;
import zouran.service.EarthquakeInfoService;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * EarthquakeInfo Api
 * Author：zouran
 * Date：2023/3/31  21:47
 * Description:
 */
@RestController
@RequestMapping("/earthquakeInfo")
@Validated
public class EarthquakeInfoController {
    private final EarthquakeInfoService earthquakeInfoService;
    private final ResultMap<EarthquakeInfo> resultMap;

    public EarthquakeInfoController(EarthquakeInfoService earthquakeInfoService, ResultMap<EarthquakeInfo> resultMap) {
        this.earthquakeInfoService = earthquakeInfoService;
        this.resultMap = resultMap;
    }

    /**
     * 全部地震信息
     */
    @RequestMapping("/getAllEarthquakeInfo")
    ResultMap getAllEarthquakeInfo() {
        return resultMap.success(earthquakeInfoService.getAllEarthquakeInfo(null));
    }

    /**
     * 全部地震坐标
     */
    @RequestMapping("/getAllEarthquakePositionInfo")
    ResultMap getAllEarthquakePositionInfo() {
        return resultMap.success(earthquakeInfoService.getAllEarthquakeInfoPosition(null));
    }


    /**
     * 范围内地震信息
     *
     * @param west  西经
     * @param east  东经
     * @param north 北纬
     * @param south 南纬
     * @return 区域内地震列表信息
     */
    @RequestMapping("/getSeismicZonesEarthquakeInfo")
    ResultMap getSeismicZonesEarthquakeInfo(@RequestParam("west") @Min(0) @Max(180) Double west, @RequestParam("east") @Min(0) @Max(180) Double east, @RequestParam("north") @Min(0) @Max(90) Double north, @RequestParam("south") @Min(0) @Max(90) Double south) {
        return resultMap.success(earthquakeInfoService.getSeismicZonesEarthquakeInfo(east, south, west, north));
    }


    /**
     * id获取地震信息
     *
     * @param earthquakeId 地震编号
     * @return 该地震详细信息
     */
    @RequestMapping("/getEarthquakeInfoById")
    ResultMap getEarthquakeById(@RequestParam("earthquakeId") @DefaultValue("2") @NotNull Long earthquakeId) {
        if (!earthquakeInfoService.judgeEarthquakeInfoExistById(earthquakeId))
            throw new MyRunTimeException(ErrorMessage.EARTHQUAKE_ID_ERROR.getMessage());
        return resultMap.success(earthquakeInfoService.getEarthquakeInfoById(earthquakeId));
    }

    /**
     * 条件获取地震信息
     */
    @RequestMapping("/getEarthquakeInfoByCondition")
    ResultMap getEarthquakeByCondition(@RequestBody @Valid SelectCondition selectCondition) {
        System.out.println(selectCondition);
        return resultMap.success(earthquakeInfoService.getEarthquakeInfoByCondition(selectCondition));
    }

    /**
     * 条件获取地震坐标
     */
    @RequestMapping("/getEarthquakePositionInfoByCondition")
    ResultMap getEarthquakePositionInfoByCondition(@RequestBody @Valid SelectCondition selectCondition) {
        //cesium地图上需要全部地震位置
        selectCondition.setPage(null);
        return resultMap.success(earthquakeInfoService.getEarthquakePositionInfoByCondition(selectCondition));
    }


    /**
     * 按名字搜历史地震
     *
     * @param name 关键词
     * @return 地震列表
     */
    @RequestMapping("/getHistoryEarthquakeInfoList")
    public ResultMap getHistoryEarthquakeInfoList(@RequestParam("EarthquakeName") String name) {
        return resultMap.success(earthquakeInfoService.getHistoryEarthquakeInfoList(name));
    }


    /**
     * 坐标获取地震
     *
     * @param lon 经度
     * @param lat 纬度
     * @return 该地震信息
     */
    @RequestMapping("/getEarthquakeInfoByPosition")
    public ResultMap getEarthquakeInfoByPosition(@RequestParam("longitude") Double lon, @RequestParam("latitude") Double lat) {
        return resultMap.success(earthquakeInfoService.getEarthquakeInfoByPosition(lon, lat));
    }


    /**
     * 范围内地震坐标
     *
     * @param west  西经
     * @param east  东经
     * @param north 北纬
     * @param south 南纬
     * @return 区域内地震列表位置信息
     */
    @RequestMapping("/getSeismicZonesEarthquakeInfoPosition")
    public ResultMap getSeismicZonesEarthquakeInfoPosition(@RequestParam("west") @Min(0) @Max(180) Double west, @RequestParam("east") @Min(0) @Max(180) Double east, @RequestParam("north") @Min(0) @Max(90) Double north, @RequestParam("south") @Min(0) @Max(90) Double south) {
        return resultMap.success(earthquakeInfoService.getSeismicZonesEarthquakeInfoPosition(east, south, west, north));
    }

    @GetMapping("/getEarthquakeInfostaticByyear")
    public ResultMap getearthquakeinfostaticbyyear(@RequestParam Integer year ){
        return resultMap.success(earthquakeInfoService.getEarthquakeInfoByyear(year));
    }



}
